package application.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @Auther: Ohto_Ai
 * @Date: 2022/2/22 - 02 - 22 - 12:34
 * @Description: utils
 * @version: 1.0
 */
public class Utils implements ProjectUtils  {
    @Override
    public Connection JDBCConection(String JDBC_DRIVER, String DB_URL, String USER, String PASS)throws Exception {

        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        return conn;
    }
    public JSONObject getJSONObject(HttpServletRequest request) throws IOException {

        InputStreamReader inputStreamReader = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder str = new StringBuilder();
        int respInt = inputStreamReader.read();
        while (respInt!=-1){
            str.append((char) respInt);
            respInt = inputStreamReader.read();
        }
        String jsonString =String.valueOf(str);
        JSONObject jsonObject=JSONObject.parseObject(jsonString);
        System.out.println(jsonString);
        System.out.println(jsonObject);
        return jsonObject;
    }
}
